package com.gome.ss.flowcrawl.plugin;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.bft.BitFetchTable;
import com.gome.ss.common.AppConfiguer;
import com.gome.ss.common.FlowCrawlHbaseHandle;
import com.gome.ss.common.Utils;
import com.gome.ss.core.datapipe.DataPipeInterface;
import com.gome.ss.core.download.SpiderDataMap;
import com.gome.ss.core.download.SpiderRequestInterface;
import com.gome.ss.core.download.SpiderResponseInterface;
import com.gome.ss.core.download.StartRequest;
import com.gome.ss.core.plugin.SpiderLocalPluginStepConfig;
import com.gome.ss.core.plugin.SpiderPluginInteface;

public class FlowCrawlEnteyPage implements SpiderPluginInteface {
	private static Logger logger = Logger.getLogger(FlowCrawlEnteyPage.class);
	FlowCrawlDataPipe dataPipe = null;
	SpiderLocalPluginStepConfig config = null;
	private BitFetchTable fetchTable = null;
	String taskID = "";
    
	@Override
	public boolean init() {
		// TODO Auto-generated method stub
		dataPipe = new FlowCrawlDataPipe(config.getPluginName());
		if (config.isUseIncrementalFilter()) {
			fetchTable = new BitFetchTable("flowcrawl-incremental-"+config.getPluginName());
			fetchTable.createMemTable();
		}
		return true;
	}
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		if (AppConfiguer.flowcrawlUseHbaseTransit)
			FlowCrawlHbaseHandle.createTable(config.getPluginName());
		else
			dataPipe.clean();
		
		if (fetchTable != null)
			fetchTable.clearTable();
	}
	
	@Override
	public boolean onStartWithIncrementalMode() {
		// TODO Auto-generated method stub
		return true;
	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		if (config.getCallbackLink() != null && !config.getCallbackLink().equals("")) {
			logger.info("Callback link : " + config.getCallbackLink());
			Utils.sendGet(config.getCallbackLink());
		}
	}

	@Override
	public List<StartRequest> getStartRequest() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean beforDownLoad(SpiderRequestInterface request) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onDownLoadFinished(SpiderResponseInterface response)
			throws Exception {
		// TODO Auto-generated method stub
		FlowCrawlSpiderCommon.onDownLoadFinished(response, config);
	}

	@Override
	public List<SpiderRequestInterface> parseLink(
			SpiderResponseInterface response) throws Exception {
		// TODO Auto-generated method stub
		List<SpiderRequestInterface> requests = FlowCrawlSpiderCommon.parseLink(response, config);
		
		if (!config.isUseIncrementalFilter() || requests == null)
			return requests;
		
		List<SpiderRequestInterface> newRequests = new ArrayList<SpiderRequestInterface>();
		for (SpiderRequestInterface request : requests) {
			if (request.isPageReady() || !request.isIncremental()) {
				newRequests.add(request);
				continue;
			}
			synchronized (FlowCrawlStepPage.class) {
				if (!fetchTable.testBit(request.getIncrementalID())) {
					logger.debug("set " + request.getIncrementalID() + " | " + request.getUrl().getLink());
					newRequests.add(request);
					fetchTable.setBit(request.getIncrementalID());
				} else {
					logger.info("Incremental test false : " + request.getIncrementalID() + " | " + request.getUrl().getLink());
				}
			}
		}
		
		return newRequests;
	}

	@Override
	public SpiderDataMap parseData(SpiderResponseInterface response)
			throws Exception {
		// TODO Auto-generated method stub
		SpiderDataMap dataMap = FlowCrawlSpiderCommon.parseData(response, config);
		return dataMap;
	}

	@Override
	public DataPipeInterface datePipe() {
		// TODO Auto-generated method stub
		return dataPipe;
	}

	@Override
	public void setConfig(Object config) {
		// TODO Auto-generated method stub
		this.config = (SpiderLocalPluginStepConfig) config;
	}

	@Override
	public void destory() {
		// TODO Auto-generated method stub
		if (fetchTable != null) {
			fetchTable.destoryTable();
		}
	}

}
